package com.polaris.live.viewmodel.home

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.CollectionUtils
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.ApiResponse
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.homeService
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.base.new
import com.polaris.live.resp.event.BannerPromptEvent
import com.polaris.live.widget.refresh.OnListListener
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.greenrobot.eventbus.EventBus

/**
 * HotManViewModel
 *
 * @author Created by lucas on 2023/10/17 14:40
 * @since 1.0.0
 */
class HotViewModel : BaseViewModel(), OnListListener {

    var pageType = HOT_LIST
    var bannerPosition = 0

    var listLiveData = MutableLiveData<ResultState<ListResp<Any>>>()

    override fun onRefresh() {
        getList(0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getList(pageIndex)
    }

    private fun getList(pageIndex: Int) {
        request({
            if (pageIndex == 0) {
                coroutineScope {
                    val listDef = async { fetchList(pageIndex) }
                    val bannerDef = async { homeService.getBannerListV2(CommonConst.BannerType.BANNER_HOME) }

                    val listResp = listDef.await()
                    val bannerResp = bannerDef.await()

                    if (bannerResp.isSuccess()) {
                        EventBus.getDefault().post(BannerPromptEvent(bannerResp.result?.notification))
                    }

                    val list = listResp.result?.list?.toMutableList<Any>()?.apply {
                        val bannerBean = ListWithBannerBean(bannerResp.result?.banner ?: emptyList())
                        if (bannerPosition >= size) {
                            add(bannerBean)
                        } else {
                            add(bannerPosition, bannerBean)
                        }
                    } ?: emptyList()
                    if (CollectionUtils.isEmpty(bannerResp.result?.banner)) {
                        listResp.new(listResp.result?.list)
                    } else {
                        listResp.new(list)
                    }
                }
            } else {
                fetchList(pageIndex).let { it.new(it.result?.list) }
            }
        }, listLiveData)
    }

    private suspend fun fetchList(pageIndex: Int): ApiResponse<ListResp<DisplayUserBean>> {
        return if (pageType == HOT_LIST) {
            homeService.getHotList(pageIndex)
        } else {
            homeService.getNewcomerList(pageIndex)
        }
    }

    val mBackpackNewLiveData = MutableLiveData<ResultState<Boolean>>()
    /**
     * getBackpackNew
     */
    fun getBackpackNew() {
        request({ userService.getBackpackNew(null, null) }, mBackpackNewLiveData)
    }

    companion object {

        const val HOT_LIST = 0
        const val NEW_LIST = 1

    }
}